package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.KnowledgeBaseDao;
import com.zja.model.KnowledgeBase;

import java.util.List;

public class KnowledgeBaseDaoImpl extends BaseDAO implements KnowledgeBaseDao {
    @Override
    public List<KnowledgeBase> selectAll() {
        String sql = "SELECT * FROM knowledge_base";
        try {
            return executeQuery(KnowledgeBase.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public KnowledgeBase selectById(Long id) {
        String sql = "SELECT * FROM knowledge_base WHERE id = ?";
        try {
            return executeQueryBean(KnowledgeBase.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(KnowledgeBase knowledgeBase) {
        String sql = "INSERT INTO knowledge_base(name, description, creator_id, admin_id, access_level, created_at, updated_at) VALUES(?, ?, ?, ?, ?, ?, ?)";
        try {
            return executeUpdate(sql, knowledgeBase.getName(), knowledgeBase.getDescription(), knowledgeBase.getCreatorId(),
                    knowledgeBase.getAdminId(), knowledgeBase.getAccessLevel(), knowledgeBase.getCreatedAt(), knowledgeBase.getUpdatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(KnowledgeBase knowledgeBase) {
        String sql = "UPDATE knowledge_base SET name = ?, description = ?, creator_id = ?, admin_id = ?, access_level = ?, updated_at = ? WHERE id = ?";
        try {
            return executeUpdate(sql, knowledgeBase.getName(), knowledgeBase.getDescription(), knowledgeBase.getCreatorId(),
                    knowledgeBase.getAdminId(), knowledgeBase.getAccessLevel(), knowledgeBase.getUpdatedAt(), knowledgeBase.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        String sql = "DELETE FROM knowledge_base WHERE id = ?";
        try {
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
